//
// Created by HC_yyt on 2021/7/22/022.
//

#include "facedetectorHandler.h"
#include "btmpHandler.h"
#include "facedetector.h"
#include "android/bitmap.h"
#define LI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)

/**
 * 在有人脸的图片上画方框
 * @param env
 * @param thiz
 * @param bitmap
 * @return bitmap
 */
jobject detectorfacehandler(JNIEnv *env, jobject bitmap) {
    AndroidBitmapInfo  bitmapInfo;
    void *pixelscolor;
    int ret;
    if((ret = AndroidBitmap_getInfo(env,bitmap,&bitmapInfo)) < 0){
        LI(" Android bitmp_getinfo error %d",ret);
        return NULL;
    }
    if(bitmapInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888){
        LI("Bitmap info fromat error");
        return NULL;
    }
    if((ret = AndroidBitmap_lockPixels(env,bitmap,&pixelscolor)) <0){
        LI("Android lockPixels error %d ",ret);
        return NULL;
    }
    cv::Mat src(bitmapInfo.height,bitmapInfo.width,CV_8UC4,pixelscolor);
    std::vector<cv::Mat> outdsts = facedetector::detectorface(src);
    if(outdsts.size()>0){
        LI("检测到人脸");
    } else{
        LI("未检测到人脸");
    }
    jclass java_bitmap_class = (jclass) env->FindClass("android/graphics/Bitmap");
    jmethodID mid = env->GetMethodID(java_bitmap_class, "getConfig","()Landroid/graphics/Bitmap$Config;");
    jobject bitmap_config = env->CallObjectMethod(bitmap, mid);
    return mat2bitmap(env,src,false,bitmap_config);
}